package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.activity.weex.StartWeexActivityUtils;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.DFHomeNovelContentBeans;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.util.GlideCircleTransform;
import com.taptech.doufu.util.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class AuthorFiveItemView extends LinearLayout {
    private TextView tvTitle;
    private View llMore, llRule;
    private String moreUrl;
    private String title;
    private String type;
    private String pageType = "";

    private List<DFHomeNovelBeans> list;

    public AuthorFiveItemView(Context context) {
        super(context);
    }

    public AuthorFiveItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AuthorFiveItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private View root_layout;
    private View linearLayout0;
    private TextView tv_more;
    private View topSpacView;
    private Context context;
    private View rl_dfnovel_layout1;
    private EventBean eventBean;

    public AuthorFiveItemView(final Context context, final DFHomeNovelContentBeans model, final String type,String pageType) {
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.author_five_view, null);
        this.context = context;
        this.type = type;
        this.pageType = pageType;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        rl_dfnovel_layout1 = view.findViewById(R.id.rl_dfnovel_layout1);
        topSpacView = view.findViewById(R.id.home_dfnovel_layout_space);
        tv_more =  view.findViewById(R.id.tv_more);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle =  view.findViewById(R.id.tvTitle);
        llMore = view.findViewById(R.id.llMore);
        llRule = view.findViewById(R.id.llRule);
        tvTitle.setText(model.getTitle());
        moreUrl = model.getMore_url();
        title = model.getTitle();
        eventBean = model.getEventData();
        initContentView(view);
        changeDayModelBg();
        list = model.getList();
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addRightTopBtnEvent(context, eventBean, type,pageType);
                if (moreUrl != null && moreUrl.contains("/weex/")) {
                    SimpleWeexActivity.startActivity(context, moreUrl);
                } else {
                    DfhomeNovelAdapter3.goToNovelMoreActivity(context, moreUrl, title, type);
                }
            }
        });
        llRule.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, BrowseActivity.class);
                intent.putExtra(Constant.URL, model.getMore_url());
                intent.putExtra(Constant.TITLE, model.getTitle());
                context.startActivity(intent);
            }
        });
        if (model.getType() == 1) { //更多
            llMore.setVisibility(VISIBLE);
            llRule.setVisibility(GONE);
        } else if (model.getType() == 3) {//规则
            llMore.setVisibility(GONE);
            llRule.setVisibility(VISIBLE);
        } else {  //不显示
            llMore.setVisibility(GONE);
            llRule.setVisibility(GONE);
        }
        setData(list);
        addView(view);
    }


    public AuthorFiveItemView(final Context context, final ChoiceTopicBean model) {
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.author_five_view, null);
        this.context = context;
        linearLayout0 = view.findViewById(R.id.home_dfnovel_layout0);
        rl_dfnovel_layout1 = view.findViewById(R.id.rl_dfnovel_layout1);
        topSpacView = view.findViewById(R.id.home_dfnovel_layout_space);
        tv_more =  view.findViewById(R.id.tv_more);
        root_layout = view.findViewById(R.id.root_layout);
        tvTitle =  view.findViewById(R.id.tvTitle);
        llMore = view.findViewById(R.id.llMore);
        llRule = view.findViewById(R.id.llRule);
        tvTitle.setText(model.getTitle());
        moreUrl = model.getMoreUrl();
        title = model.getTitle();
        eventBean = model.getEventData();
        initContentView(view);
        changeDayModelBg();
        list = model.getAuthorList();
        llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengEventUtil.addRightTopBtnEvent(context, eventBean, type,pageType);
                if (moreUrl != null && moreUrl.contains("/weex/")) {
                    SimpleWeexActivity.startActivity(context, moreUrl);
                } else {
                    DfhomeNovelAdapter3.goToNovelMoreActivity(context, moreUrl, title, type);
                }
            }
        });
        llRule.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, BrowseActivity.class);
                intent.putExtra(Constant.URL, model.getMoreUrl());
                intent.putExtra(Constant.TITLE, model.getTitle());
                context.startActivity(intent);
            }
        });
        if (model.getType() == 1) { //更多
            llMore.setVisibility(VISIBLE);
            llRule.setVisibility(GONE);
        } else if (model.getType() == 3) {//规则
            llMore.setVisibility(GONE);
            llRule.setVisibility(VISIBLE);
        } else {  //不显示
            llMore.setVisibility(GONE);
            llRule.setVisibility(GONE);
        }
        setData(list);
        addView(view);
    }

    private TextView title_1, title_2, title_3, title_4, title_5;
    private TextView tvDesc_1, tvDesc_2, tvDesc_3, tvDesc_4, tvDesc_5;
    private ImageView ivPic_1, ivPic_2, ivPic_3, ivPic_4, ivPic_5;
    private View lay_1, lay_2, lay_3, lay_4, lay_5;

    public void initContentView(View view) {
        title_1 =  view.findViewById(R.id.title_1);
        ivPic_1 =  view.findViewById(R.id.ivPic_1);
        title_2 =  view.findViewById(R.id.title_2);
        ivPic_2 =  view.findViewById(R.id.ivPic_2);
        title_3 =  view.findViewById(R.id.title_3);
        ivPic_3 =  view.findViewById(R.id.ivPic_3);

        title_4 =  view.findViewById(R.id.title_4);
        ivPic_4 =  view.findViewById(R.id.ivPic_4);

        title_5 =  view.findViewById(R.id.title_5);
        ivPic_5 =  view.findViewById(R.id.ivPic_5);
        tvDesc_1 =  view.findViewById(R.id.tvDesc_1);
        tvDesc_2 =  view.findViewById(R.id.tvDesc_2);
        tvDesc_3 =  view.findViewById(R.id.tvDesc_3);
        tvDesc_4 =  view.findViewById(R.id.tvDesc_4);
        tvDesc_5 =  view.findViewById(R.id.tvDesc_5);
        lay_1 = view.findViewById(R.id.lay_1);
        lay_2 = view.findViewById(R.id.lay_2);
        lay_3 = view.findViewById(R.id.lay_3);
        lay_4 = view.findViewById(R.id.lay_4);
        lay_5 = view.findViewById(R.id.lay_5);
    }

    private void setData(List<DFHomeNovelBeans> list) {
        if (list != null) {
            switch (list.size()) {
                case 0:
                    lay_1.setVisibility(GONE);
                    lay_2.setVisibility(GONE);
                    lay_3.setVisibility(GONE);
                    lay_4.setVisibility(GONE);
                    lay_5.setVisibility(GONE);
                    break;
                case 1:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(GONE);
                    lay_3.setVisibility(GONE);
                    lay_4.setVisibility(GONE);
                    lay_5.setVisibility(GONE);
                    break;
                case 2:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(GONE);
                    lay_4.setVisibility(GONE);
                    lay_5.setVisibility(GONE);
                    break;
                case 3:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(VISIBLE);
                    lay_4.setVisibility(GONE);
                    lay_5.setVisibility(GONE);
                    break;
                case 4:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(VISIBLE);
                    lay_4.setVisibility(VISIBLE);
                    lay_5.setVisibility(GONE);
                    break;
                case 5:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(VISIBLE);
                    lay_4.setVisibility(VISIBLE);
                    lay_5.setVisibility(VISIBLE);
                    break;
                default:
                    lay_1.setVisibility(VISIBLE);
                    lay_2.setVisibility(VISIBLE);
                    lay_3.setVisibility(VISIBLE);
                    lay_4.setVisibility(VISIBLE);
                    lay_5.setVisibility(VISIBLE);
                    break;
            }
            for (int i = 0; i < list.size(); i++) {
                final DFHomeNovelBeans model = list.get(i);
                if (model != null) {
                    if (i == 0) {
                        setItemUI(model, ivPic_1, lay_1, title_1, tvDesc_1);
                    } else if (i == 1) {
                        setItemUI(model, ivPic_2, lay_2, title_2, tvDesc_2);
                    } else if (i == 2) {
                        setItemUI(model, ivPic_3, lay_3, title_3, tvDesc_3);
                    } else if (i == 3) {
                        setItemUI(model, ivPic_4, lay_4, title_4, tvDesc_4);
                    } else if (i == 4) {
                        setItemUI(model, ivPic_5, lay_5, title_5, tvDesc_5);
                    } else {
                        break;
                    }
                }
            }
        }
    }


    private void setItemUI(final DFHomeNovelBeans model, ImageView ivPic, View lay, TextView title, TextView tvDesc) {

        if (model != null && model.getUser() != null) {
            title.setText(model.getUser().getNickname());
            if (!TextUtils.isEmpty(model.getUser().getIcon())) {
                String url = model.getUser().getIcon();
                url = Constant.changeUrlHost(url);
                Glide.with(context)
                        .load(url)
                        .centerCrop()
                        .dontAnimate()
                        .transform(new GlideCircleTransform(context, 1, Color.parseColor("#e5e5e5")))
                        .into(ivPic);

            }
            if (!TextUtils.isEmpty(model.getDesc())) {
                tvDesc.setVisibility(VISIBLE);
                tvDesc.setText(model.getDesc());
            } else {
                tvDesc.setVisibility(GONE);
            }
            lay.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    UmengEventUtil.addContentEvent(context, eventBean, type,pageType);
                    if (model.getUser() != null) {
                        UserDetailActivity.Companion.startActivity(context,model.getUser().getUid());
                    }
                }
            });
        }

    }

    public void refrush(DFHomeNovelContentBeans model) {
        if (model == null || !TextUtils.isEmpty(model.getTitle()) && model.getTitle().equals(title)) {
            return;
        }
        moreUrl = model.getMore_url();
        title = model.getTitle();
        list = model.getList();
        if (llMore != null) {
            if (!TextUtils.isEmpty(model.getMore_url())) {
                llMore.setVisibility(VISIBLE);
            } else {
                llMore.setVisibility(GONE);
            }
        }
        if (tvTitle != null) {
            tvTitle.setText(model.getTitle());
        }
        setData(list);
    }

    public void changeDayModelBg() {
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            darkBg();
        } else {
            dayBg();
        }
    }


    private void dayBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.home_list_item_color));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.white));
        tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_1));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_color_6C788A));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.divider_line_F1F6F9));
        rl_dfnovel_layout1.setBackgroundColor(context.getResources().getColor(R.color.white));

        setItemDayBg(lay_1, title_1, ivPic_1);
        setItemDayBg(lay_2, title_2, ivPic_2);
        setItemDayBg(lay_3, title_3, ivPic_3);
        setItemDayBg(lay_4, title_4, ivPic_4);
        setItemDayBg(lay_5, title_5, ivPic_5);

    }

    private void setItemDayBg(View lay, TextView title, ImageView ivPic) {
        lay.setBackgroundColor(context.getResources().getColor(R.color.white));
        title.setTextColor(context.getResources().getColor(R.color.text_color_727885));
        ivPic.setAlpha(1.0f);
    }

    private void setItemNigntBg(View lay, TextView title, ImageView ivPic) {
        lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        title.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        ivPic.setAlpha(1.0f);
    }

    private void darkBg() {
        root_layout.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        linearLayout0.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
        topSpacView.setBackgroundColor(context.getResources().getColor(R.color.bg_dark));
        tv_more.setTextColor(context.getResources().getColor(R.color.text_dark));
        rl_dfnovel_layout1.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));

        setItemNigntBg(lay_1, title_1, ivPic_1);
        setItemNigntBg(lay_2, title_2, ivPic_2);
        setItemNigntBg(lay_3, title_3, ivPic_3);
        setItemNigntBg(lay_4, title_4, ivPic_4);
        setItemNigntBg(lay_5, title_5, ivPic_5);
    }

}
